package com.online.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.online.music.entity.po.*;
import com.online.music.mapper.*;
import com.online.music.service.IUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.music.utils.UserContextUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

import com.online.music.common.Result;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Leno
 * @Date: 2025-02-10
 * @Description: UserFollowServiceImpl
 */
@Service
@RequiredArgsConstructor
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements IUserFollowService {

    private final UserFollowMapper userFollowMapper;

    private final PlaylistMapper playlistMapper;

    private final SongMapper songMapper;

    private final AlbumMapper albumMapper;

    private final UserMapper userMapper;

    @Override
    public Result<Page<User>> findPage(Integer pageNum, Integer pageSize, UserFollow userFollow) {
        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> qw = new QueryWrapper<>();
        // 查询用户关注的人
        List<UserFollow> userFollows = userFollowMapper.selectList(
                new LambdaQueryWrapper<UserFollow>()
                        .eq(UserFollow::getUserId, UserContextUtil.getUserId())
        );
        List<Long> collectIds = userFollows.stream().map(UserFollow::getFollowedUserId).collect(Collectors.toList());
        // Ensure the list is not empty before adding the IN clause
        if (!collectIds.isEmpty()) {
            qw.in("u.id", collectIds);
        } else {
            // If no followed users, you might want to handle this case,
            // for example, by returning an empty result
            return Result.success(new Page<>());
        }
        qw.eq("u.status", 1);
        page = userFollowMapper.findPage(page, qw);
        return Result.success(page);
    }

    @Override
    @Transactional
    public Result<String> deleteBatch(List<Long> ids) {
        // 删除前先判断是否存在...
        removeByIds(ids);
        return Result.successMsg("删除成功");
    }

    @Override
    public Result<Page<Playlist>> followPlaylist(UserFollow userFollow) {
        List<Playlist> playlists = playlistMapper.selectList(new LambdaQueryWrapper<Playlist>()
                .eq(userFollow.getFollowedUserId() != null,
                        Playlist::getUserId, userFollow.getFollowedUserId()
                )
        );
        Page<Playlist> page = new Page<>();
        page.setRecords(playlists);
        return Result.success(page);
    }

    @Override
    public Result<Page<Album>> followAlbum(UserFollow userFollow) {
        List<Album> albums = albumMapper.selectList(new LambdaQueryWrapper<Album>()
                .eq(userFollow.getFollowedUserId() != null,
                        Album::getUserId, userFollow.getFollowedUserId()
                )
        );
        Page<Album> page = new Page<>();
        page.setRecords(albums);
        return Result.success(page);
    }

    @Override
    public Result<Page<Song>> followSong(UserFollow userFollow) {
        List<Song> songs = songMapper.selectList(new LambdaQueryWrapper<Song>()
                .eq(userFollow.getFollowedUserId() != null,
                        Song::getUserId, userFollow.getFollowedUserId()
                )
        );
        Page<Song> page = new Page<>();
        page.setRecords(songs);
        return Result.success(page);
    }

}

